<!DOCTYPE html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="威龙的博客">
    <meta name="keyword" content="随缘">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        python获取中文首字母 - 威龙的博客 | VeyronC&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/aircloud.css">
    <link rel="stylesheet" href="/css/gitment.css">
    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
</head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 心若水， 自非凡。该博客已迁移至 https://www.cnblogs.com/xrszff </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar">
            <img src="/img/avatar.jpg">
        </div>
        <div class="name">
            <i>威龙</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li>
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li>
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li>
                <a href="/archive">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li>
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#pinyin-包"><span class="toc-text">pinyin 包</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#简单字的方法"><span class="toc-text">简单字的方法</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input">
            <span id="begin-search">搜索</span>
        </div>
        <div style="padding:10px 20px;font=10;">
            若搜索无反应请更换浏览器~
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>
        <div class="index-about-mobile">
            <i> 心若水， 自非凡。该博客已迁移至 https://www.cnblogs.com/xrszff </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        python获取中文首字母
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2019-01-05 11:37:00</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#python" title="python">python</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>

    </div>
    <div class="post-content no-indent">
        <h1 id="pinyin-包"><a href="#pinyin-包" class="headerlink" title="pinyin 包"></a>pinyin 包</h1><figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pinyin</span><br><span class="line">pinyin.<span class="built_in">get</span>(<span class="string">'暨南'</span>)</span><br></pre></td></tr></table></figure>
<h1 id="简单字的方法"><a href="#简单字的方法" class="headerlink" title="简单字的方法"></a>简单字的方法</h1><p>但是这种方法复杂字的检测有问题，比如 暨</p>
<figure class="highlight autoit"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">def single_get_first(unicode1):</span><br><span class="line">    str1 = unicode1.encode(<span class="string">'gbk'</span>)</span><br><span class="line">    try:</span><br><span class="line">        ord(str1)</span><br><span class="line">        <span class="keyword">return</span> str1</span><br><span class="line">    except:</span><br><span class="line">        <span class="built_in">asc</span> = str1[<span class="number">0</span>] * <span class="number">256</span> + str1[<span class="number">1</span>] - <span class="number">65536</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-20319</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-20284</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'a'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-20283</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-19776</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'b'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-19775</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-19219</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'c'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-19218</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-18711</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'d'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-18710</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-18527</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'e'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-18526</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-18240</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'f'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-18239</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-17923</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'g'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-17922</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-17418</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'h'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-17417</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-16475</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'j'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-16474</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-16213</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'k'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-16212</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-15641</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'l'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-15640</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-15166</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'m'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-15165</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-14923</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'n'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-14922</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-14915</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'o'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-14914</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-14631</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'p'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-14630</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-14150</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'q'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-14149</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-14091</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'r'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-14090</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-13119</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'s'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-13118</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-12839</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'t'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-12838</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-12557</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'w'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-12556</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-11848</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'x'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-11847</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-11056</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'y'</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">asc</span> &gt;= <span class="number">-11055</span> <span class="literal">and</span> <span class="built_in">asc</span> &lt;= <span class="number">-10247</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'z'</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">''</span></span><br><span class="line">     </span><br><span class="line">def getPinyin(<span class="built_in">string</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">string</span>==None:</span><br><span class="line">        <span class="keyword">return</span> None</span><br><span class="line">    lst = list(<span class="built_in">string</span>)</span><br><span class="line">    charLst = []</span><br><span class="line">    <span class="keyword">for</span> l <span class="keyword">in</span> lst:</span><br><span class="line">        charLst.append(single_get_first(l))</span><br><span class="line">    <span class="keyword">return</span>  <span class="string">''</span>.join(charLst)</span><br><span class="line"> </span><br><span class="line"><span class="keyword">if</span> __name__==<span class="string">'__main__'</span>:</span><br><span class="line">    print(getPinyin(<span class="string">'非场'</span>))</span><br></pre></td></tr></table></figure>
<p>参考：</p>
<ul>
<li><a href="https://www.cnblogs.com/mayibanjiah/p/6007473.html" target="_blank" rel="noopener">https://www.cnblogs.com/mayibanjiah/p/6007473.html</a></li>
</ul>

        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src data-src="/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br>
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        
        <li>
            <a target="_blank" href="http://weibo.com/2983694150">
                            <span class="fa-stack fa-lg">
                                  <i class="iconfont icon-weibo"></i>
                            </span>
            </a>
        </li>
        

        

        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a href="https://www.10000h.top">10000H</a></span>
        <span>/</span>
        
        <span><a href="https://niexiaotao.com">Xiaotao&#39;s Page</a></span>
        <span>/</span>
        
        <span><a href="#">It helps SEO</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a href="https://hexo.io/">Hexo</a>  Theme <a href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="/js/index.js"></script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


    <script>
        /**
         *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
         *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables
        */
        if( '' || '')
        var disqus_config = function () {
            this.page.url = '';  // Replace PAGE_URL with your page's canonical URL variable
            this.page.identifier = ''; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
        };

        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document, s = d.createElement('script');
            s.src = 'https://airclouds-blog.disqus.com/embed.js';
            s.setAttribute('data-timestamp', +new Date());
            (d.head || d.body).appendChild(s);
        })();
    </script>



</html>
